package com.feng.tiku.controller;

import com.feng.common.dto.PageDto;
import com.feng.common.vo.ApiRest;
import com.feng.common.vo.PageResult;
import com.feng.tiku.entity.Repo;
import com.feng.tiku.service.RepoService;
import com.feng.tiku.vo.RepoHistogram;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author feng
 * @since 2023-03-20 11:03:08
 */
@RestController
@RequestMapping("/tiku/repo")
@CrossOrigin
@Api(tags = "题库模块")
public class RepoController {

    @Autowired
    private RepoService repoService;

    @PostMapping("page")
    @ApiOperation("分页条件查询")
    public PageResult<Repo> listRepo(@RequestBody PageDto<String> pageDto) {
        return repoService.listRepo(pageDto);
    }

    @PostMapping
    @ApiOperation("添加||修改题库")
    @Transactional
    public ApiRest insertRepo(@RequestBody Repo repo) {
        return repoService.saveOrUpdate(repo) ?
                ApiRest.success() : ApiRest.failure();

    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id单个删除")
    @Transactional
    public ApiRest deleteRepo(@PathVariable String id) {
        return repoService.removeById(id) ?
                ApiRest.success() : ApiRest.failure();
    }

    @DeleteMapping("/more/{ids}")
    @ApiOperation("根据id批量删除")
    @Transactional
    public ApiRest deleteRepoByIds(@PathVariable String[] ids) {
        return repoService.removeBatchByIds(Arrays.asList(ids)) ?
                ApiRest.success() : ApiRest.failure();
    }

    @ApiOperation("获取题库分布柱状图")
    @GetMapping("view")
    public ApiRest<List<RepoHistogram>> repoView() {
        return repoService.getHistogram();
    }
}
